import './common.js'
// import { showToast } from "@tarojs/taro"
const alertShow = (title)=>{}
window.alert = alertShow
/*判断客户端*/
function judgeClient() {
    let u = window.navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;   //判断是否是 android终端
    let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);     //判断是否是 
    if (isAndroid) {
        return 'Android';
    } else if (isIOS) {
        return 'IOS';
    } else {
        return 'PC';
    }
}
alert('init sdk')
const pageInit = function(resolve,reject){
if (judgeClient() === 'IOS') {
	
    function setupWebViewJavascriptBridge(callback) {
        // ios
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge);
        }
        if (window.WVJBCallbacks) {
            return window.WVJBCallbacks.push(callback);
        }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function () {
            document.documentElement.removeChild(WVJBIframe)
        }, 0)
    }

    setupWebViewJavascriptBridge(function (bridge) {
        // 获取用户信息
		try{
			bridge.callHandler('fetchToken', {}, function responseCallback(res) {
				alert('token ios======'+res)
				window.winToken = res
				resolve&&resolve(res)
			    localStorage.setItem('X-Auth0-Token', res)
			})
		}catch(err){
			reject&&reject()
		}
       
        // 获取用户信息详情
        bridge.callHandler('getLoginUser', {}, function responseCallback(res) {
            localStorage.setItem('userInformation', res)
        })
        window.bridge = bridge
    })

} else if (judgeClient() === 'Android') {
    function connectWebViewJavascriptBridge(callback) {
        // 安卓
        if (window.WebViewJavascriptBridgeAndroid) {
            callback(WebViewJavascriptBridgeAndroid)
        } else {
            document.addEventListener(
                'WebViewJavascriptBridgeAndroidReady'
                , function () {
                    callback(WebViewJavascriptBridgeAndroid)
                },
                false
            );
        }
    }

    connectWebViewJavascriptBridge(function (bridge) {
		try{
        bridge.callHandler('fetchToken', {}, function responseCallback(res) {
			window.winToken = res
			alert('token Android======'+res)
			resolve&&resolve(res)
            localStorage.setItem('X-Auth0-Token', res)
        })
		}catch(err){
			reject&&reject()
		}
        // 获取用户信息详情
        bridge.callHandler('getLoginUser', {}, function responseCallback(res) {
            localStorage.setItem('userInformation', res)
        })
        window.bridge = bridge
		console.log("bridge --sdsds",bridge)
    })
} else {
    // console.log('SDK-->')
}
}

pageInit()
export {
    pageInit
}
//  const pageInit =()=> new Promise(function (resolve,reject){
// 	 return pageInit(resolve,reject)
//  })
// export const pageInits = pageInit